import { ref } from 'vue'
import { defineStore } from 'pinia'
import { loginAPI } from '@/api/admin.js'
import { useMenuStore } from '@/stores'

export const useUserStore = defineStore(
  'user',
  () => {
    const user = ref({})

    // 登录
    const login = async (params) => {
      const result = await loginAPI(params)
      user.value = result
    }

    // 修改用户信息
    const updateUser = (params) => {
      Object.assign(user.value, params)
    }

    // 退出登录
    const logout = () => {
      user.value = { avatar: '' }
      useMenuStore().clearMenuList()
    }

    return { user, login, logout, updateUser }
  },
  { persist: true }
)
